﻿Imports controlHackCharacter
Imports SocketServidor
Imports System.IO

Public Class frmPrincipal
    Private control As controlServidorHackCharacter
    Private aTablas() As String = {"aventuras"}

    Dim WithEvents WinSockServer As New SocketServidor.SocketServidor

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        With WinSockServer
            'Establezco el puerto donde escuchar 
            .PuertoDeEscucha = 8050

            'Comienzo la escucha 
            .Escuchar()
        End With

        Me.control = New controlServidorHackCharacter("127.0.0.1", "root", "", "hackcharacter", aTablas)
        control.refrescarDataSet()
        Me.DataGridView1.DataSource = control.getDataSetPrueba("aventuras")
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim x As Integer
        Dim aventura As String
        For x = 3 To 10
            aventura = "aventura" & x.ToString
            Me.control.modificarDatos("Insert into aventuras(nombre,master,sistema) values ('" & aventura & "','master2',0)")
        Next x
        control.refrescarDataSet()
        Me.DataGridView1.DataSource = control.getDataSetPrueba("aventuras")
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.control.modificarDatos("TRUNCATE TABLE " & aTablas(0))
        control.refrescarDataSet()
        Me.DataGridView1.DataSource = control.getDataSetPrueba("aventuras")

    End Sub

    Private Sub WinSockServer_NuevaConexion(ByVal IDTerminal As System.Net.IPEndPoint) Handles WinSockServer.NuevaConexion
        'Path file
        Dim SPath As String = "E:\[Servidor]dataset.xml"

        'DataSet to xml file
        control.dataSetToXml(SPath)

        'Get text file
        Dim text As String
        text = My.Computer.FileSystem.ReadAllText(SPath)

        'Delete text file
        My.Computer.FileSystem.DeleteFile(SPath)

        'Input in text a finish flag.
        text = text & "</xml>"

        'Envio del texto a todos los clientes
        WinSockServer.EnviarDatos(text)


    End Sub

    Private Sub WinSockServer_ConexionTerminada(ByVal IDTerminal As System.Net.IPEndPoint) Handles WinSockServer.ConexionTerminada
        'Muestro con quien se termino la conexion 
        'MsgBox("Se ha desconectado el cliente desde la IP= " & IDTerminal.Address.ToString & _
        '                                                    ",Puerto = " & IDTerminal.Port)
    End Sub

    Private Sub WinSockServer_DatosRecibidos(ByVal IDTerminal As System.Net.IPEndPoint) Handles WinSockServer.DatosRecibidos
        Dim cadena() As String
        cadena = Split(WinSockServer.ObtenerDatos(IDTerminal), "</consulta>")

        control.modificarDatos(cadena(0))

        'Muestro el mensaje recibido 
        Call MsgBox(WinSockServer.ObtenerDatos(IDTerminal))
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        'Path file
        Dim SPath As String = "E:\[Servidor]dataset.xml"

        'DataSet to xml file
        control.dataSetToXml(SPath)

        'Get text file
        Dim text As String
        text = My.Computer.FileSystem.ReadAllText(SPath)

        'Delete text file
        My.Computer.FileSystem.DeleteFile(SPath)

        'Input in text a finish flag.
        text = text & "</xml>"

        'Envio del texto a todos los clientes
        WinSockServer.EnviarDatos(text)
 
    End Sub

    Public Sub New()

        ' Llamada necesaria para el Diseñador de Windows Forms.
        InitializeComponent()

        ' Agregue cualquier inicialización después de la llamada a InitializeComponent().

    End Sub
End Class
